<template>
  <div class="mobile-statistics-setting">
    <div class="header">
      <div class="left">
        <el-button type="primary" @click="handleAdd">+ 新建</el-button>
      </div>
      <div class="right">
        <el-input
          v-model="searchQuery"
          placeholder="搜索规则名称"
          prefix-icon="el-icon-search"
          clearable
          @clear="handleSearch"
          @input="handleSearch"
        />
      </div>
    </div>

    <el-table
      :data="tableData"
      border
      style="width: 100%"
      v-loading="loading"
    >
      <el-table-column
        prop="name"
        label="规则名称"
        min-width="150">
      </el-table-column>
      <el-table-column
        prop="startDate"
        label="生效月份"
        width="120">
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '已启用' ? 'success' : 'info'">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="scope"
        label="适用人员范围"
        min-width="180">
      </el-table-column>
      <el-table-column
        prop="creator"
        label="创建人"
        width="120">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作"
        width="200"
        fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新建/编辑弹窗 -->
    <el-dialog
      :title="dialogType === 'add' ? '新建统计规则' : '编辑统计规则'"
      :visible.sync="dialogVisible"
      width="600px">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="规则名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入规则名称"></el-input>
        </el-form-item>
        <el-form-item label="生效月份" prop="startDate">
          <el-date-picker
            v-model="form.startDate"
            type="month"
            placeholder="选择月份">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="form.status"
            active-text="启用"
            inactive-text="停用">
          </el-switch>
        </el-form-item>
        <el-form-item label="适用范围" prop="scope">
          <el-select v-model="form.scope" placeholder="请选择适用范围">
            <el-option label="全部" value="all"></el-option>
            <el-option label="指定部门" value="department"></el-option>
            <el-option label="指定人员" value="person"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'MobileStatisticsSetting',
  data() {
    return {
      searchQuery: '',
      loading: false,
      tableData: [
        {
          name: '默认统计规则',
          startDate: '2024-01',
          status: '已启用',
          scope: '全部',
          creator: '系统创建',
          createTime: '2024-01-01 00:00:00'
        }
      ],
      dialogVisible: false,
      dialogType: 'add', // add or edit
      form: {
        name: '',
        startDate: '',
        status: true,
        scope: 'all'
      },
      rules: {
        name: [
          { required: true, message: '请输入规则名称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        startDate: [
          { required: true, message: '请选择生效月份', trigger: 'change' }
        ],
        scope: [
          { required: true, message: '请选择适用范围', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    handleSearch() {
      // TODO: 实现搜索功能
      console.log('搜索关键词:', this.searchQuery)
    },
    handleAdd() {
      this.dialogType = 'add'
      this.form = {
        name: '',
        startDate: '',
        status: true,
        scope: 'all'
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogType = 'edit'
      this.form = { ...row }
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确认删除该统计规则吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // TODO: 调用删除API
        this.$message.success('删除成功')
      }).catch(() => {})
    },
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // TODO: 调用保存API
          this.$message.success(this.dialogType === 'add' ? '创建成功' : '更新成功')
          this.dialogVisible = false
        }
      })
    }
  }
}
</script>

<style scoped>
.mobile-statistics-setting {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.right {
  display: flex;
  gap: 10px;
}

.el-input {
  width: 200px;
}
</style>